/* ========================================================
  BernsteinBasis.cpp
  This is the bernstein polynominal evaluation routine.
  We give the Bernstein polynominal of order p, with derivative
  der = 0,1,2, at evaluation points x
 
  Author: Ju Liu
  =======================================================*/
#include <iostream>
#include <assert.h>
#include "BernsteinBasis.hpp"

BernsteinBasis::BernsteinBasis(int p, double xi)
{
  order = p;
  x = xi;
  
  der0 = new double[order+1];
  der1 = new double[order+1];
  der2 = new double[order+1];

  initialize();
};

BernsteinBasis::~BernsteinBasis()
{
  delete [] der0;
  delete [] der1;
  delete [] der2;
}

void BernsteinBasis::initialize()
{
  switch (order)
  {
    case 1:
      der0[0] = 1-x;
      der0[1] = x;
      
      der1[0] = -1.0;
      der1[1] = 1.0;

      der2[0] = 0.0;
      der2[1] = 0.0;
      break;

    case 2:
      der0[0] = (1-x)*(1-x);
      der0[1] = 2*x*(1-x);
      der0[2] = x*x;

      der1[0] = 2*(x-1);
      der1[1] = 2-4*x;
      der1[2] = 2*x;

      der2[0] = 2;
      der2[1] = -4;
      der2[2] = 2;

      break;

    case 3:
      der0[0] = (1-x)*(1-x)*(1-x);
      der0[1] = 3*x*(1-x)*(1-x);
      der0[2] = 3*x*x*(1-x);
      der0[3] = x*x*x;

      der1[0] = -3*(1-x)*(1-x);
      der1[1] = 9*x*x-12*x+3;
      der1[2] = 6*x-9*x*x;
      der1[3] = 3*x*x;

      der2[0] = -6*(x-1);
      der2[1] = 18*x-12;
      der2[2] = 6-18*x;
      der2[3] = 6*x;

      break;

    default:
       std::cerr<<"ERROR: We do not implement the bernstein polynominal with this order"<<std::endl;
       assert(0);
  }
};
